using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.TestDrivens;
using Pixysoft.Framework.Configurations.Core;
using System.IO;

namespace Pixysoft.Framework.Configurations.TestCases
{
    [TestDriven(TestDrivenType.IntegrationTest, "ConfigurationSetting")]
    class UnitTest_020_ConfigurationSetting
    {
        static IAssert assert = Assert.New(true);

        [TestStart]
        public static bool TestCaseStart()
        {

            return true;
        }

        [TestStop]
        public static void TestCaseStop()
        {
        }

        [TestStateSave]
        public static ITestDrivenDocument TestCaseStateSave()
        {
            return assert.Document;
        }

        [TestCase("Save")]
        public static void TestCase_000_Save()
        {


            string id = "NoebeConfiguration";
            string name = "name";

            ConfigurationSetting setting = new ConfigurationSetting(id, name);
            assert.IsEqual(name, setting.Name);
            assert.IsEqual(id, setting.Id);


            string nodename = "nodename";

            IConfigurationNode node1 = new ConfigurationNode(nodename);
            assert.IsEqual(nodename, node1.NodeName);

            node1.Text = "text";
            assert.IsEqual("text", node1.Text);

            node1.Base64Text = "base64text";
            assert.IsEqual("base64text", node1.Base64Text);

            string key = "key";
            node1.SetDesText(key, "destext");
            assert.IsEqual("destext", node1.GetDesText(key));
            assert.Write(node1.Text, "", "");
            assert.Write(node1.ToString());

            node1.Property.Add("property1", "property1");
            assert.IsEqual("property1", node1.Property["property1"]);
            foreach (IConfigurationProperty property in node1.Property)
            {
                assert.Write(property.Key);
                assert.Write(property.Value);
            }

            IConfigurationProperty property2 = node1.Property.Add("property2");
            assert.IsEqual("property2", node1.Property[1].Key);

            property2.Base64Value = "base64value";
            assert.IsEqual("base64value", property2.Base64Value);

            property2.SetDesValue(key, "desvalue");
            assert.IsEqual("desvalue", property2.GetDesValue(key));
            assert.IsEqual(2, node1.Property.Count);

            string filename = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory) + "Pixysoft.dll";
            string classname = "Pixysoft.ByteBuilder";
            node1.Property.Add(StringHelper.default_attribute_filename, filename);
            node1.Property.Add(StringHelper.default_attribute_classname, classname);
            assert.IsInstanceOfType(typeof(Pixysoft.ByteBuilder), node1.Create<Pixysoft.ByteBuilder>());

            setting.Node.Add(node1);

            IConfigurationNode node2 = new ConfigurationNode(nodename);
            try
            {
                setting.Node.Add(node2);
            }
            catch (Exception ex)
            {
                assert.Write(ex.Message);
            }

            setting.Nodes.Add(node2);
            assert.IsEqual(2, setting.Node.Count);
            assert.IsEqual(2, setting.Nodes.Count);

            assert.Write(setting.Serialize());
            assert.Write(setting.Serialize(true));

            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string savefilename = path + StringHelper.default_config_filename;
            setting.Save(savefilename);
            assert.IsTrue(File.Exists(savefilename));

            string saveobjname = path + "testobj.config";
            setting.SaveAsObject(saveobjname);
            assert.IsTrue(File.Exists(saveobjname));
            assert.Write(File.ReadAllText(saveobjname));

            Assert.Cache.Save("1", setting);
        }
    }
}
